package com.immoc.jdbc.hrapp.command;

import com.immoc.jdbc.common.DbUtils;
import com.immoc.jdbc.hrapp.entity.Employee;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Paginaction implements  Command {
    @Override
    public void execute() {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入页号:");
        int page = in.nextInt();
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Employee> list = new ArrayList();
        try {
            conn = DbUtils.getConnection();
            String sql = "select * from employee limit ?,10";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,(page-1)*10);
            rs = ps.executeQuery();
            while (rs.next()){
                Integer eno = rs.getInt("eno");
                String ename = rs.getString("ename");
                Float salary = rs.getFloat("salary");
                String dname = rs.getString("dname");
                Date hireDate = rs.getDate("hireDate");
                Employee emp = new Employee();
                emp.setDname(dname);
                emp.setEno(eno);
                emp.setEname(ename);
                emp.setSalary(salary);
                emp.setHireDate(hireDate);
                list.add(emp);
                System.out.println(emp.getEname());
                System.out.println(emp.getHireDate());
            }

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DbUtils.closeConnection(conn);
        }

    }
}
